//+------------------------------------------------------------------+
//|                                                    mapattern.mq4 |
//|                                         Dimitri Rudel 27.01.2014 |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Dimitri Rudel 27.01.2014"
#property link      ""

#include <basic.mqh>
#include <pattern.mqh>
#include <tradeManager.mqh>



//deviation of low or high to the MA %
extern double exMaxDev = 0.0;
extern int exMaxRsi = 70;
extern int exMinRsi = 30;

extern int exMa1 = 30;
extern int exMa2 = 90;
extern int exMa3 = 200;
extern int exRsi = 12;

extern int exMaS = 0;

extern double exMaGrade = 0.001;

extern double exTakeProfit = 100;
extern double exTakeProfitFactor = 1;

datetime oldTimeBase;

string eaName = "maPattern";

int exMa[3];



int OnInit()
  {
   exMagicNumber += Period();
   fake();  
   
   exMa[0]=exMa1;
   exMa[1]=exMa2;
   exMa[2]=exMa3;
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//----
   
//----

  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
void OnTick(void)
  {
   OnDeinit(0);
   //Print(MarketInfo(Symbol(),MODE_DIGITS));
    if(oldTimeBase == Time[0]){
 
    }else{    
    oldTimeBase =  Time[0];

double spread = MarketInfo(Symbol(),MODE_SPREAD)/10;

      bool signal = false;
      bool reversion = false;
      bool pattern = false;
      int patternForm = 0;



      //mgr_trailVoigt();
      //mgr_trailByMa();

      //initialisation
      double ma1 = iMA(NULL,0,exMa1,1,MODE_SMA,PRICE_CLOSE,1);
      double ma1Old =  iMA(NULL,0,exMa1,1,MODE_SMA,PRICE_CLOSE,2);
      double ma2 = iMA(NULL,0,exMa2,1,MODE_SMA,PRICE_CLOSE,1);
      double ma3 = iMA(NULL,0,exMa3,1,MODE_SMA,PRICE_CLOSE,1);
      double rsi = iRSI(NULL,0,exRsi,PRICE_CLOSE,1);
      
      double ma[3];
      double maOld[3];
      double maGrade[3];
      
      for(int i = 0;i<=3;i++){
         ma[i] = iMA(NULL,0,exMa[i],1,MODE_SMA,PRICE_CLOSE,1);
         maOld[i] = iMA(NULL,0,exMa[i],1,MODE_SMA,PRICE_CLOSE,2);
         
         if(maOld[i]>0){
            maGrade[i] = (ma[i] - maOld[i]) / maOld[i] * 100;
         }else{
            maGrade[i] = 0;
         }
      }
      


      
      if((Low[1] > ma[exMaS]) && Low[2] > ma[exMaS] && Low[3] > ma[exMaS] && (Low[3]-ma[exMaS])*2 < getRodSize(3)){

         if(p_isBullish(1)){  // && maGrade[exMaS] >= exMaGrade){
            reversion = true;
          
         }
      }else if((High[1] < ma[exMaS]) && High[2] < ma[exMaS] && High[3] < ma[exMaS] && (ma[exMaS]-High[3])*2 < getRodSize(3)){
         if(p_isBearish(1)){  // && maGrade[exMaS] <= exMaGrade * -1){
            reversion = true;
         }
      }

      if(reversion){
         patternForm = p_getSignal();
//signal = p_getSignal();
         //b_debug("getSignal:"+patternForm);
         if(patternForm == 1 ){
            pattern = true;
         //}else if(){
                  //          signal = true;
         }else{
            pattern = false;
         }
      }

 
      if(pattern){
    
       signal = p_isBullish(1)  || p_isBearish(1);
                  //extended, oder bestimmte reichweite zum ma2 und ma3 bsp: (price -sl) * 2 < ma2 - price
                  //signal = (p_isBullish(1) && ma1 < Close[1] && ma2 < Close[1] && ma3 < Close[1]);
                  //                     || (b_isBullish(1) && ma1 > Close[1] && ma2 < Close[1] && ma3 > Close[1]);
                  //extended2
                 // signal = signal && (p_isBullish(1) || p_isBearish(1));
      }
      //signal = 1;

      if(signal && b_isTradeTime()){
                  //draw();

         double sl = 0;
         int cmd = -1;
         double price = Ask;
         double tp;
         double slDiff;

         if(p_isBullish(1)){
            price = Ask;
            cmd = 0;
            slDiff = MathAbs(price-Low[3]);
            //sl = price - slDiff;
            sl = Low[3]-spread;

         }else if(p_isBearish(1)){
            price = Bid;
            cmd = 1;
            slDiff = MathAbs(High[3]-price);
            //sl = price + slDiff;
            sl = High[3]+spread;
          }
         
         if(cmd == 0){
            tp = price + slDiff*exTakeProfitFactor;   
         }else{
            tp = price - slDiff*exTakeProfitFactor;
         }
         
         if(cmd >=0){
            drawV(oldTimeBase);
            b_OrderSend(Symbol(),cmd,0.1,price,3000,sl,tp,eaName+"["+Symbol()+"]"+"["+exMagicNumber+"]",exMagicNumber,0); 
         }
      }
   }

  }



  void drawV(datetime dt,string name = "line",color col = Red){ 
   if(!exDebug){
      
   } 
   
   ObjectCreate(name+dt,OBJ_VLINE,0,dt,0);
   ObjectSet(name+dt,OBJPROP_STYLE,STYLE_DOT);  
   ObjectSet(name+dt,OBJPROP_COLOR,col);
}//+------------------------------------------------------------------+